Adding patched EXIF data display on image page, based on include file by Vinay Yadav
[lhc/web/wiklou.git] / includes / ImagePage.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 /**
7 *
8 */
9 if( !defined( 'MEDIAWIKI' ) )
10 die();
11
12 require_once( 'Image.php' );
13
14 if ( $wgShowEXIF ) {
15 require_once ( 'exifReader.inc' ) ;
16 }
17
18 /**
19 * Special handling for image description pages
20 * @package MediaWiki
21 */
22 class ImagePage extends Article {
23
24 /* private */ var $img; // Image object this page is shown for
25
26 function view() {
27 global $wgUseExternalEditor, $wgOut ;
28
29 $this->img = new Image( $this->mTitle );
30
31 if( $this->mTitle->getNamespace() == NS_IMAGE ) {
32 if ( $this->img->exists() ) $this->showEXIFdata();
33 $this->openShowImage();
34
35 # No need to display noarticletext, we use our own message, output in openShowImage()
36 if ( $this->getID() ) {
37 Article::view();
38 } else {
39 # Just need to set the right headers
40 $wgOut->setArticleFlag( true );
41 $wgOut->setRobotpolicy( 'index,follow' );
42 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
43 $wgOut->addMetaTags();
44 $this->viewUpdates();
45 }
46
47 if ( $this->img->exists() ) {
48 $this->uploadNewVersionLink();
49 if ( $wgUseExternalEditor && $this->img->exists() ) {
50 $this->externalEditorLink();
51 }
52 }
53 $this->closeShowImage();
54 $this->imageHistory();
55 $this->imageLinks();
56 } else {
57 Article::view();
58 }
59 }
60
61 function showEXIFdata()
62 {
63 global $wgOut , $wgShowEXIF ;
64 if ( ! $wgShowEXIF ) return ;
65 $file = $this->img->getImagePath () ;
66 $per = new phpExifReader ( $file ) ;
67 $per->processFile () ;
68
69 $r = "<table border='1' cellspacing='0' cellpadding='0' align='right'>" ;
70 $r .= "<caption>EXIF data</caption>" ;
71 $a = $per->getImageInfo() ;
72 if ( count ( $a ) == 0 ) return ; # No EXIF data
73 unset ( $a["FileName"] ) ;
74 unset ( $a["Thumbnail"] ) ;
75 foreach ( $a AS $k => $v ) {
76 $r .= "<tr>" ;
77 $v = str_replace ( "\0" , "" , $v ) ;
78 $r .= "<th><small>" . htmlspecialchars ( $k ) . "</small></th>" ;
79 $r .= "<td><small>" . htmlspecialchars ( $v ) . "</small></td>" ;
80 $r .= "</tr>" ;
81 }
82 $r .= "</table>" ;
83 $wgOut->addHTML ( $r ) ;
84 }
85
86 function openShowImage()
87 {
88 global $wgOut, $wgUser, $wgImageLimits, $wgRequest,
89 $wgUseImageResize, $wgRepositoryBaseUrl,
90 $wgUseExternalEditor, $wgServer;
91 $full_url = $this->img->getViewURL();
92 $anchoropen = '';
93 $anchorclose = '';
94
95 if( $wgUser->getOption( 'imagesize' ) == '' ) {
96 $sizeSel = User::getDefaultOption( 'imagesize' );
97 } else {
98 $sizeSel = IntVal( $wgUser->getOption( 'imagesize' ) );
99 }
100 if( !isset( $wgImageLimits[$sizeSel] ) ) {
101 $sizeSel = User::getDefaultOption( 'imagesize' );
102 }
103 $max = $wgImageLimits[$sizeSel];
104 $maxWidth = $max[0];
105 $maxHeight = $max[1];
106 $sk = $wgUser->getSkin();
107
108 if ( $this->img->exists() ) {
109 if ( $this->img->getType() != '' ) {
110 # image
111 $width = $this->img->getWidth();
112 $height = $this->img->getHeight();
113 $msg = wfMsg('showbigimage', $width, $height, intval( $this->img->getSize()/1024 ) );
114 if ( $width > $maxWidth ) {
115 $height = floor( $height * $maxWidth / $width );
116 $width = $maxWidth;
117 }
118 if ( $height > $maxHeight ) {
119 $width = floor( $width * $maxHeight / $height );
120 $height = $maxHeight;
121 }
122 if ( $width != $this->img->getWidth() || $height != $this->img->getHeight() ) {
123 if( $wgUseImageResize ) {
124 $thumbnail = $this->img->getThumbnail( $width );
125 $url = $thumbnail->getUrl();
126 } else {
127 # No resize ability? Show the full image, but scale
128 # it down in the browser so it fits on the page.
129 $url = $full_url;
130 }
131 $anchoropen = "<a href=\"{$full_url}\">";
132 $anchorclose = "</a><br />\n$anchoropen{$msg}</a>";
133 } else {
134 $url = $full_url;
135 }
136 $s = '<div class="fullImageLink">' . $anchoropen .
137 "<img border=\"0\" src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"" .
138 htmlspecialchars( $wgRequest->getVal( 'image' ) ).'" />' . $anchorclose . '</div>';
139 } else {
140 $s = "<div class=\"fullMedia\">" . $sk->makeMediaLink( $this->img->getName(),'' ) . '</div>';
141 }
142 $wgOut->addHTML( $s );
143 if($this->img->fromSharedDirectory) {
144 $sharedtext="<div class=\"sharedUploadNotice\">" . wfMsg("sharedupload");
145 if($wgRepositoryBaseUrl) {
146 $sharedtext .= " ". wfMsg("shareduploadwiki",$wgRepositoryBaseUrl . urlencode($this->mTitle->getDBkey()));
147 }
148 $sharedtext.="</div>";
149 $wgOut->addWikiText($sharedtext);
150 }
151
152 } else {
153 # Image does not exist
154 $wgOut->addWikiText( wfMsg( 'noimage', $this->getUploadUrl() ) );
155 }
156 }
157
158 function getUploadUrl() {
159 global $wgServer;
160 $uploadTitle = Title::makeTitle( NS_SPECIAL, 'Upload' );
161 return $wgServer . $uploadTitle->getLocalUrl( 'wpDestFile=' . urlencode( $this->img->getName() ) );
162 }
163
164
165 function uploadNewVersionLink() {
166 global $wgOut;
167 $wgOut->addWikiText( wfMsg( 'uploadnewversion', $this->getUploadUrl() ) );
168 }
169
170 function externalEditorLink()
171 {
172 global $wgUser,$wgOut;
173 $sk = $wgUser->getSkin();
174 $wgOut->addHTML( '<div class="editExternally">' );
175 $wgOut->addHTML( $sk->makeKnownLinkObj( $this->mTitle,
176 wfMsg( 'edit-externally' ),
177 "action=edit&externaledit=true&mode=file" ) );
178 $wgOut->addWikiText( '<div class="editExternallyHelp">' .
179 wfMsg('edit-externally-help') . '</div>' );
180 $wgOut->addHTML( '</div><br clear="both" />' );
181 }
182
183 function closeShowImage()
184 {
185 # For overloading
186
187 }
188
189 /**
190 * If the page we've just displayed is in the "Image" namespace,
191 * we follow it with an upload history of the image and its usage.
192 */
193 function imageHistory()
194 {
195 global $wgUser, $wgOut;
196
197 $sk = $wgUser->getSkin();
198
199 $line = $this->img->nextHistoryLine();
200
201 if ( $line ) {
202 $list =& new ImageHistoryList( $sk );
203 $s = $list->beginImageHistoryList() .
204 $list->imageHistoryLine( true, $line->img_timestamp,
205 $this->mTitle->getDBkey(), $line->img_user,
206 $line->img_user_text, $line->img_size, $line->img_description );
207
208 while ( $line = $this->img->nextHistoryLine() ) {
209 $s .= $list->imageHistoryLine( false, $line->img_timestamp,
210 $line->oi_archive_name, $line->img_user,
211 $line->img_user_text, $line->img_size, $line->img_description );
212 }
213 $s .= $list->endImageHistoryList();
214 } else { $s=''; }
215 $wgOut->addHTML( $s );
216 }
217
218 function imageLinks()
219 {
220 global $wgUser, $wgOut;
221
222 $wgOut->addHTML( '<h2>' . wfMsg( 'imagelinks' ) . "</h2>\n" );
223
224 $dbr =& wfGetDB( DB_SLAVE );
225 $page = $dbr->tableName( 'page' );
226 $imagelinks = $dbr->tableName( 'imagelinks' );
227
228 $sql = "SELECT page_namespace,page_title FROM $imagelinks,$page WHERE il_to=" .
229 $dbr->addQuotes( $this->mTitle->getDBkey() ) . " AND il_from=page_id"
230 . " LIMIT 500"; # quickie emergency brake
231 $res = $dbr->query( $sql, "ImagePage::imageLinks" );
232
233 if ( 0 == $dbr->numRows( $res ) ) {
234 $wgOut->addHtml( '<p>' . wfMsg( "nolinkstoimage" ) . "</p>\n" );
235 return;
236 }
237 $wgOut->addHTML( '<p>' . wfMsg( 'linkstoimage' ) . "</p>\n<ul>" );
238
239 $sk = $wgUser->getSkin();
240 while ( $s = $dbr->fetchObject( $res ) ) {
241 $name = Title::MakeTitle( $s->page_namespace, $s->page_title );
242 $link = $sk->makeKnownLinkObj( $name, "" );
243 $wgOut->addHTML( "<li>{$link}</li>\n" );
244 }
245 $wgOut->addHTML( "</ul>\n" );
246 }
247
248 function delete()
249 {
250 global $wgUser, $wgOut, $wgRequest;
251
252 $confirm = $wgRequest->getBool( 'wpConfirmB' );
253 $image = $wgRequest->getVal( 'image' );
254 $oldimage = $wgRequest->getVal( 'oldimage' );
255
256 # Only sysops can delete images. Previously ordinary users could delete
257 # old revisions, but this is no longer the case.
258 if ( !$wgUser->isAllowed('delete') ) {
259 $wgOut->sysopRequired();
260 return;
261 }
262 if ( wfReadOnly() ) {
263 $wgOut->readOnlyPage();
264 return;
265 }
266
267 # Better double-check that it hasn't been deleted yet!
268 $wgOut->setPagetitle( wfMsg( 'confirmdelete' ) );
269 if ( ( !is_null( $image ) )
270 && ( '' == trim( $image ) ) ) {
271 $wgOut->fatalError( wfMsg( 'cannotdelete' ) );
272 return;
273 }
274
275 $this->img = new Image( $this->mTitle );
276
277 # Deleting old images doesn't require confirmation
278 if ( !is_null( $oldimage ) || $confirm ) {
279 if( $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
280 $this->doDelete();
281 } else {
282 $wgOut->fatalError( wfMsg( 'sessionfailure' ) );
283 }
284 return;
285 }
286
287 if ( !is_null( $image ) ) {
288 $q = '&image=' . urlencode( $image );
289 } else if ( !is_null( $oldimage ) ) {
290 $q = '&oldimage=' . urlencode( $oldimage );
291 } else {
292 $q = '';
293 }
294 return $this->confirmDelete( $q, $wgRequest->getText( 'wpReason' ) );
295 }
296
297 function doDelete()
298 {
299 global $wgOut, $wgUser, $wgContLang, $wgRequest;
300 global $wgUseSquid, $wgInternalServer, $wgPostCommitUpdateList;
301 $fname = 'ImagePage::doDelete';
302
303 $reason = $wgRequest->getVal( 'wpReason' );
304 $oldimage = $wgRequest->getVal( 'oldimage' );
305
306 $dbw =& wfGetDB( DB_MASTER );
307
308 if ( !is_null( $oldimage ) ) {
309 if ( strlen( $oldimage ) < 16 ) {
310 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
311 return;
312 }
313 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
314 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
315 return;
316 }
317
318 # Invalidate description page cache
319 $this->mTitle->invalidateCache();
320
321 # Squid purging
322 if ( $wgUseSquid ) {
323 $urlArr = Array(
324 $wgInternalServer.wfImageArchiveUrl( $oldimage ),
325 $wgInternalServer.$this->mTitle->getFullURL()
326 );
327 wfPurgeSquidServers($urlArr);
328 }
329 $this->doDeleteOldImage( $oldimage );
330 $dbw->delete( 'oldimage', array( 'oi_archive_name' => $oldimage ) );
331 $deleted = $oldimage;
332 } else {
333 $image = $this->mTitle->getDBkey();
334 $dest = wfImageDir( $image );
335 $archive = wfImageDir( $image );
336
337 # Delete the image file if it exists; due to sync problems
338 # or manual trimming sometimes the file will be missing.
339 $targetFile = "{$dest}/{$image}";
340 if( file_exists( $targetFile ) && ! @unlink( $targetFile ) ) {
341 # If the deletion operation actually failed, bug out:
342 $wgOut->fileDeleteError( $targetFile );
343 return;
344 }
345 $dbw->delete( 'image', array( 'img_name' => $image ) );
346 $res = $dbw->select( 'oldimage', array( 'oi_archive_name' ), array( 'oi_name' => $image ) );
347
348 # Purge archive URLs from the squid
349 $urlArr = Array();
350 while ( $s = $dbw->fetchObject( $res ) ) {
351 $this->doDeleteOldImage( $s->oi_archive_name );
352 $urlArr[] = $wgInternalServer.wfImageArchiveUrl( $s->oi_archive_name );
353 }
354
355 # And also the HTML of all pages using this image
356 $linksTo = $this->img->getLinksTo();
357 if ( $wgUseSquid ) {
358 $u = SquidUpdate::newFromTitles( $linksTo, $urlArr );
359 array_push( $wgPostCommitUpdateList, $u );
360 }
361
362 $dbw->delete( 'oldimage', array( 'oi_name' => $image ) );
363
364 # Image itself is now gone, and database is cleaned.
365 # Now we remove the image description page.
366
367 $article = new Article( $this->mTitle );
368 $article->doDeleteArticle( $reason ); # ignore errors
369
370 # Invalidate parser cache and client cache for pages using this image
371 # This is left until relatively late to reduce lock time
372 Title::touchArray( $linksTo );
373
374 /* Delete thumbnails and refresh image metadata cache */
375 $this->img->purgeCache();
376
377
378 $deleted = $image;
379 }
380
381 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
382 $wgOut->setRobotpolicy( 'noindex,nofollow' );
383
384 $sk = $wgUser->getSkin();
385 $loglink = $sk->makeKnownLinkObj(
386 Title::makeTitle( NS_SPECIAL, 'Log/delete' ),
387 wfMsg( 'deletionlog' ) );
388
389 $text = wfMsg( 'deletedtext', $deleted, $loglink );
390
391 $wgOut->addHTML( '<p>' . $text . "</p>\n" );
392 $wgOut->returnToMain( false, $this->mTitle->getPrefixedText() );
393 }
394
395 function doDeleteOldImage( $oldimage )
396 {
397 global $wgOut;
398
399 $name = substr( $oldimage, 15 );
400 $archive = wfImageArchiveDir( $name );
401
402 # Delete the image if it exists. Sometimes the file will be missing
403 # due to manual intervention or weird sync problems; treat that
404 # condition gracefully and continue to delete the database entry.
405 # Also some records may end up with an empty oi_archive_name field
406 # if the original file was missing when a new upload was made;
407 # don't try to delete the directory then!
408 #
409 $targetFile = "{$archive}/{$oldimage}";
410 if( $oldimage != '' && file_exists( $targetFile ) && !@unlink( $targetFile ) ) {
411 # If we actually have a file and can't delete it, throw an error.
412 $wgOut->fileDeleteError( "{$archive}/{$oldimage}" );
413 } else {
414 # Log the deletion
415 $log = new LogPage( 'delete' );
416 $log->addEntry( 'delete', $this->mTitle, wfMsg('deletedrevision',$oldimage) );
417 }
418 }
419
420 function revert()
421 {
422 global $wgOut, $wgRequest, $wgUser;
423 global $wgUseSquid, $wgInternalServer, $wgDeferredUpdateList;
424
425 $oldimage = $wgRequest->getText( 'oldimage' );
426 if ( strlen( $oldimage ) < 16 ) {
427 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
428 return;
429 }
430 if ( strstr( $oldimage, "/" ) || strstr( $oldimage, "\\" ) ) {
431 $wgOut->unexpectedValueError( 'oldimage', htmlspecialchars($oldimage) );
432 return;
433 }
434
435 if ( wfReadOnly() ) {
436 $wgOut->readOnlyPage();
437 return;
438 }
439 if( $wgUser->isAnon() ) {
440 $wgOut->errorpage( 'uploadnologin', 'uploadnologintext' );
441 return;
442 }
443 if ( ! $this->mTitle->userCanEdit() ) {
444 $wgOut->sysopRequired();
445 return;
446 }
447 if( !$wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ), $oldimage ) ) {
448 $wgOut->errorpage( 'internalerror', 'sessionfailure' );
449 return;
450 }
451 $name = substr( $oldimage, 15 );
452
453 $dest = wfImageDir( $name );
454 $archive = wfImageArchiveDir( $name );
455 $curfile = "{$dest}/{$name}";
456
457 if ( ! is_file( $curfile ) ) {
458 $wgOut->fileNotFoundError( htmlspecialchars( $curfile ) );
459 return;
460 }
461 $oldver = wfTimestampNow() . "!{$name}";
462
463 $dbr =& wfGetDB( DB_SLAVE );
464 $size = $dbr->selectField( 'oldimage', 'oi_size', array( 'oi_archive_name' => $oldimage ) );
465
466 if ( ! rename( $curfile, "${archive}/{$oldver}" ) ) {
467 $wgOut->fileRenameError( $curfile, "${archive}/{$oldver}" );
468 return;
469 }
470 if ( ! copy( "{$archive}/{$oldimage}", $curfile ) ) {
471 $wgOut->fileCopyError( "${archive}/{$oldimage}", $curfile );
472 }
473
474 # Record upload and update metadata cache
475 $img = Image::newFromName( $name );
476 $img->recordUpload( $oldver, wfMsg( "reverted" ) );
477
478 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
479 $wgOut->setRobotpolicy( 'noindex,nofollow' );
480 $wgOut->addHTML( wfMsg( 'imagereverted' ) );
481
482 $descTitle = $img->getTitle();
483 $wgOut->returnToMain( false, $descTitle->getPrefixedText() );
484 }
485 }
486
487 /**
488 * @todo document
489 * @package MediaWiki
490 */
491 class ImageHistoryList {
492 function ImageHistoryList( &$skin ) {
493 $this->skin =& $skin;
494 }
495
496 function beginImageHistoryList() {
497 $s = "\n<h2>" . wfMsg( 'imghistory' ) . "</h2>\n" .
498 "<p>" . wfMsg( 'imghistlegend' ) . "</p>\n".'<ul class="special">';
499 return $s;
500 }
501
502 function endImageHistoryList() {
503 $s = "</ul>\n";
504 return $s;
505 }
506
507 function imageHistoryLine( $iscur, $timestamp, $img, $user, $usertext, $size, $description ) {
508 global $wgUser, $wgLang, $wgContLang, $wgTitle;
509
510 $datetime = $wgLang->timeanddate( $timestamp, true );
511 $del = wfMsg( 'deleteimg' );
512 $delall = wfMsg( 'deleteimgcompletely' );
513 $cur = wfMsg( 'cur' );
514
515 if ( $iscur ) {
516 $url = Image::imageUrl( $img );
517 $rlink = $cur;
518 if ( $wgUser->isAllowed('delete') ) {
519 $link = $wgTitle->escapeLocalURL( 'image=' . $wgTitle->getPartialURL() .
520 '&action=delete' );
521 $style = $this->skin->getInternalLinkAttributes( $link, $delall );
522
523 $dlink = '<a href="'.$link.'"'.$style.'>'.$delall.'</a>';
524 } else {
525 $dlink = $del;
526 }
527 } else {
528 $url = htmlspecialchars( wfImageArchiveUrl( $img ) );
529 if( $wgUser->getID() != 0 && $wgTitle->userCanEdit() ) {
530 $token = urlencode( $wgUser->editToken( $img ) );
531 $rlink = $this->skin->makeKnownLinkObj( $wgTitle,
532 wfMsg( 'revertimg' ), 'action=revert&oldimage=' .
533 urlencode( $img ) . "&wpEditToken=$token" );
534 $dlink = $this->skin->makeKnownLinkObj( $wgTitle,
535 $del, 'action=delete&oldimage=' . urlencode( $img ) .
536 "&wpEditToken=$token" );
537 } else {
538 # Having live active links for non-logged in users
539 # means that bots and spiders crawling our site can
540 # inadvertently change content. Baaaad idea.
541 $rlink = wfMsg( 'revertimg' );
542 $dlink = $del;
543 }
544 }
545 if ( 0 == $user ) {
546 $userlink = $usertext;
547 } else {
548 $userlink = $this->skin->makeLinkObj(
549 Title::makeTitle( NS_USER, $usertext ),
550 $usertext );
551 }
552 $nbytes = wfMsg( 'nbytes', $size );
553 $style = $this->skin->getInternalLinkAttributes( $url, $datetime );
554
555 $s = "<li> ({$dlink}) ({$rlink}) <a href=\"{$url}\"{$style}>{$datetime}</a>"
556 . " . . {$userlink} ({$nbytes})";
557
558 $s .= $this->skin->commentBlock( $description, $wgTitle );
559 $s .= "</li>\n";
560 return $s;
561 }
562
563 }
564
565
566 ?>